#ifndef MACFRAMETIMER_H
#define MACFRAMETIMER_H

#include <LDF/FrameTimer.h>
#include "../ToolkitLayer.h"

/**
 * This is a Mac OS X implemetation of the FrameTimer class, used to caclulate the frame rate when rendering components in the UofC Large Display Framework.
 *
 * This class measures the time spent during a single rendering pass and stores the value of this time "interval" within the #intervals queue.
 * The average of all elements in the queue thereby provides a relatively smoothed FPS measurement.
 *
 * It uses the Core Foundation function CFAbsoluteTimeGetCurrent to obtain the current time.
 *
 * This implementation was based on the frame rate calculations used in the
 * TabletopVis class of the original implementation of the UofC Tabletop
 * Framework by A. Miede and T. Isenberg in the Interactions Lab at the
 * University of Calgary, Canada.
 *
 * @author Matthew Tobiasz - tobiasz@ucalgary.ca
 * @since December, 21, 2007
 */

class TOOLKITLAYER_API MacFrameTimer : public FrameTimer
{
	// Attributes
private:
	/** The start time of the interval being measured. */
	double startTime;
	/** The end time of the interval being measured. */
	double endTime;
	/** The average of the intervals being measured. */
	double averageInterval;
	/** The maximum number of intervals to be considered. */
	unsigned int maxIntervals;
	/** The sum of all interval values. */
	double sumIntervals;
	/** The queue of interval values used to calculate the average value. */
	std::queue<double> intervals;

	// Methods
public:
	/**
	 * Constructs this Mac frame timer object. If no value is given to the maximum
	 * number of intervals, a defualt value of 10 is used.
	 *
	 * @param maxNumberIntervals the value to be set as the maximum number of
	 * intervals in the average calcualtions.
	 */
	MacFrameTimer(unsigned int maxNumberIntervals = 10);
	
	/**
	 * Destroys this Mac frame timer object.
	 */
	~MacFrameTimer(void);

	/**
	 * Starts measuring an interval. This is the implementation of the virtual function in the abstract FrameTimer class.
	 */
	void start();
	
	/**
	 * Ends measuring an interval. This is the implementation of the pure virtual function in the abstract FrameTimer class.
	 *
	 * This method adds the value of the recently concluded interval to the #intervals queue and, updates the #sumIntervals and #averageInterval attributes.
	 */
	void end();
	
	/**
	 * Calculates the frame per seconds value, using the value of the #averageInterval attribute.
	 *
	 * @return the fps value based on the current interval values.
	 */
	double calculateFrameRate();
};

#endif // MACFRAMETIMER_H
